package edu.cs.usask.ca.silver.businesslogic.util;

import javax.transaction.Status;
import javax.transaction.UserTransaction;

import com.atomikos.icatch.jta.UserTransactionImp;

/**
 * Transaction Manager for Business Logic layer.
 * @author Yudi Xue
 *
 */
public class SilverBLTransactionManager {

	public static void startTransaction() throws Exception {
		UserTransaction utx = new UserTransactionImp();
		utx.setTransactionTimeout(200);
		
		utx.begin();
	}
	
	public static void closeTransaction(boolean error) throws Exception {
		UserTransaction utx = new UserTransactionImp();
		if(utx.getStatus() != Status.STATUS_NO_TRANSACTION) {
			if(error)
				utx.rollback();
			else
				utx.commit();
		} 
		// else close transaction is called outside a tx.
			
	}
	
}
